package cn.icanci.ladybird.service.admin.impl;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.bo.AdminUploadCarouselBO;
import cn.icanci.ladybird.pojo.entity.Carousel;
import cn.icanci.ladybird.service.CarouselService;
import cn.icanci.ladybird.service.admin.AdminUploadCarouselService;
import cn.icanci.ladybird.service.util.NginxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author: icanci
 * @date: Created in 2020/10/7 0:37
 * @classAction: 轮播图上传实现类
 */
@Service
public class AdminUploadCarouselServiceImpl implements AdminUploadCarouselService {
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private NginxService nginxService;

    @Override
    public JsonData uploadCarousel(HttpServletRequest request, AdminUploadCarouselBO adminUploadCarouselBO) {
        try {
            System.out.println(adminUploadCarouselBO);
            // 用来接收对象
            Carousel carousel = new Carousel();
            // 设置非文件数据
            Integer adminId = (Integer) request.getAttribute("admin_id");
            carousel.setCarouselAdminId(Long.valueOf(adminId));
            carousel.setCarouselWeight(Integer.valueOf(adminUploadCarouselBO.getCarouselWeight()));
            carousel.setCarouselEnable(Integer.valueOf(adminUploadCarouselBO.getCarouselStatus()));
            carousel.setCarouselImgLink(adminUploadCarouselBO.getCarouselAddress());
            carousel.setCarouselTitle(adminUploadCarouselBO.getCarouselName());
            carousel.setCreateTime(new Date());
            carousel.setUpdateTime(new Date());
            // 文件上传
            Object path = nginxService.uploadPicture(adminUploadCarouselBO.getFile());
            carousel.setCarouselImgUrl(path.toString());
            System.out.println(carousel);
            carouselService.insert(carousel);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.builderError(e.getLocalizedMessage());
        }
        return JsonData.builderSuccess();
    }
}
